package com.wisewill.wakeonlan;

import com.wisewill.wakeonlan.common.AppService;
import com.wisewill.wakeonlan.common.PropertyUtil;
import com.wisewill.wakeonlan.common.WakeOnLan;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private Button btnSave;
	private Button btnWake;
	private EditText editIp;
	private EditText editMac;
	
	private void initApp(){
		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AppService.save(editMac.getText().toString(), editIp.getText().toString());
				AppService.alert(v.getContext(), "存储成功");
			}
		});
		
		btnWake.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					WakeOnLan.wake(editMac.getText().toString()
							, editIp.getText().toString()
							, 7);
					AppService.alert(v.getContext(), "唤醒完毕");
				}catch(Exception e){
					AppService.alert(v.getContext(), "唤醒异常：" + e.getMessage());
				}
			}
		});
		
		editIp.setText(AppService.readIp());
		editMac.setText(AppService.readMac());
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnSave = (Button)this.findViewById(R.id.btnSave);
		btnWake = (Button)this.findViewById(R.id.btnWake);
		editIp = AppService.get(this, R.id.editIp);
		editMac = AppService.get(this, R.id.editMac);
		
		initApp();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
